package cn.edu.nyist.integration.controller;

import cn.edu.nyist.integration.common.Const;
import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.User;
import cn.edu.nyist.integration.service.IntegrationService;
import cn.edu.nyist.integration.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private IntegrationService integrationService;


    /**
     * 用户信息分页显示
     * @param pageNum
     * @param pageSize
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/user_page")
    public String userListPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                               @RequestParam(value = "username",required = false) String username,
                               HttpSession session, Model model){
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/user/";
        }
        /*管理员查询到的用户等级都是普通用户*/

        PageInfo pageInfo = userService.selectUsers(pageNum, pageSize,username, Const.Role.ROLE_CUSTOMER);
        model.addAttribute("pageList",pageInfo);

        return "page/member-manager";
    }


    /**
     * 删除用户
     * @param id
     * @param attributes
     * @return
     */
    @RequestMapping("/user_del")
    public String deleteUser(@RequestParam Long id, RedirectAttributes attributes){

        ServerResponse response = userService.delUserById(id);
        if (!response.isSuccess()){
            //删除失败
            String msg = response.getMsg();
            attributes.addFlashAttribute("errorMsg",msg);
        }

        return "redirect:/admin/user_page";
    }

    @RequestMapping("/add_user_page")
    public String toAddUserPage(){
        return "page/user-add";
    }


    /**
     * 管理员添加用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/user_add",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addUser(User user){
        //todo select user_info to insert
        ServerResponse<String> serverResponse = userService.register(user);
        if (serverResponse.isSuccess()){
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return serverResponse;
    }

    /**
     * 跳转到用户编辑界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/user_edit")
    public String showUserInfo(@RequestParam Long id,Model model){
        if (id != null){
            ServerResponse<User> response = userService.selectById(id.toString());
            if (response.isSuccess()){
                User data = response.getData();
                model.addAttribute("userData",data);
                return "page/user_edit";
            }
        }
       return "redirect:/admin/user_page";
    }

    /**
     * 修改用户状态
     * @param username
     * @param phone
     * @param status
     * @return
     */
    @RequestMapping(value = "/update_status",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateUserStatus(@RequestParam String username,
                                           @RequestParam String phone,
                                           @RequestParam Integer status){

        return userService.updateStatus(username, phone, status);
    }


}
